<template>
  <div class="myContainer">
    <el-row class="myBorderBottom myMarginBottom_10px">
      <el-col :span="7" class="myTitleText">基本信息</el-col>
    </el-row>

    <el-row :gutter="10" class="myMarginBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        <span class="require myVA_M">*</span>
        考核模板名称 :
      </el-col>
      <el-col :span="6">
        <el-input placeholder="考核模板名称" v-model="template_name" :readonly="readonly"></el-input>
      </el-col>
    </el-row>

    <el-row :gutter="10" class="myMarginBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        <span class="require myVA_M">*</span>
        模板类型 :
      </el-col>
      <el-col :span="6" class="myLineHeight_40px">
        <template>
          <el-radio-group v-model="templateType">
            <el-radio :label="'1'">职能</el-radio>
            <el-radio :label="'2'">例行</el-radio>
          </el-radio-group>
        </template>
      </el-col>
    </el-row>

    <!-- <el-row :gutter="10" class="myMarginBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        <span class="require myVA_M">*</span>
        任务责任人 :
      </el-col>
      <el-col :span="6">
        <el-input placeholder="任务责任人" v-model="personLiable" @focus="getRole" :disabled="readonly"></el-input>
      </el-col>
    </el-row> -->
    
    <el-row class="myBorderBottom myMarginBottom_10px">
      <el-col :span="7" class="myTitleText">考核类型及标准</el-col>
    </el-row>

    <el-row :gutter="10" class="myMarginBottom_10px">
      <el-col :span="24" >
        <Supervision 
          :supervisionData="supervisionData" 
          :supervisionTypeData="typeList" 
          :isdisabled="readonly"
          @getChildData="getChildData"
          v-if="flag">
        </Supervision>
      </el-col> 
    </el-row>

    <el-row class="myBorderBottom myMarginBottom_10px">
      <el-col :span="7" class="myTitleText">状态及备注</el-col>
    </el-row>

    <el-row :gutter="10" class="myMarginBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        <span class="require myVA_M">*</span>
        启用状态 :
      </el-col>
      <el-col :span="6" class="myLineHeight_40px">
        <template>
          <el-radio-group v-model="status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </template>
      </el-col>
    </el-row>

    <el-row :gutter="10" class="myMarginBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        备注 :
      </el-col>
      <el-col :span="6">
        <el-input placeholder="备注" type="textarea" v-model="template_note" :readonly="readonly"></el-input>
      </el-col>
    </el-row>

    <el-row :gutter="10" class="myMarginBottom_10px">
      <el-col :span="6" :offset="3">
        <el-button type="primary" v-if="type=='add'" @click="add">新增</el-button>
        <el-button type="primary" v-if="type=='update'" @click="update">保存</el-button>
        <el-button @click="back">返回</el-button>
      </el-col>
    </el-row>
    
    <!-- 选择职务 start -->
    <el-dialog title="选择职务" class="myRoleDialog" :visible.sync="newRoleDialogVisible">
      <el-row :gutter="10">
        <el-col :span="20">
          <el-input placeholder="输入关键词" v-model="kw_role"></el-input>
        </el-col>
        <el-col :span="4" class="myTextAlignRight">
          <el-button type="primary" class="myBgGreen" @click="onSearchRole">查询</el-button>
        </el-col>
      </el-row>
      <div class="myPaddingTop_10px">
        <el-tree 
          key="roleDlg"
          class="myOV_y_400px"
          ref="roleDlg"
          v-loading="roleLoading"
          :data="roleDlgList"
          :props="roleDlgProps"
          node-key="role_id"
          @check-change="onChange"
          show-checkbox>
        </el-tree>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" class="myBgGreen" @click="newRoleDialogVisible = false">取 消</el-button>
        <el-button @click="handleRoleClick" type="primary" class="myBgGreen">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 选择职务 end -->
  </div>
</template>     
  
<style scoped>
  .myBorderBottom{
    border-bottom:1px dotted #dbdbdb;
    padding-bottom: 5px;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myContainer{
    background: #fff;
    padding: 10px;
  }
  .myTitleText{
    font-weight: 500;
    font-size: 16px;
  }
  .myLineHeight_40px{
    line-height: 40px;
  }
  .myPaddingTop_10px{
    padding-top: 10px;
  }  
  .myred{
    color: #f00;
    font-size: 12px;
  }
  .myMarginBottom_10px{
    margin-bottom: 10px;
  }
  .myLeftMsg{
    line-height: 40px;
    text-align: right;
  }
  .myVA_M{
    vertical-align: middle;
  }
  .mymarginTop_10px{
    margin-top: 10px;
  }
  .myOV_y_400px{
    border:1px solid #f2f2f2;
    box-sizing: border-box;
    min-height: 200px;
    max-height: 400px;
    overflow-y: scroll; 
  } 
</style>

<script>
  import Supervision from 'components/Supervision.vue';
  import { getRoleList } from '@/api/role';
  import { addTemplate , getTypeList , infoTemplate , updateTemplate } from '@/api/quality';
  export default {
    data(){
      return{
        title:"考核模板管理",
        type:"",
        id:"",
        readonly:false,
        status:1,
        kw_role:"",
        newRoleDialogVisible:false,
        roleDlgList:[],
        roleLoading:false,
        roleIdList:"",
        roleNameList:"",
        roleDlgProps: {
          children: 'children',
          label: 'role_name'
        }, 
        supervisionData:[],
        typeList:[],
        childData:[],
        personLiable:"",
        personLiableID:"",
        template_name:"",
        template_note:"",
        i:0,
        flag:false, 
        templateType:''
      }
    },
    components: {
      Supervision
    },
    created(){
      this.type=this.$route.query.type;
      this.id=this.$route.query.id;
      if(this.type=="add"){
        document.title="新增模板";
        this.ongetTypeList();
      }else if(this.type=="info"){
        this.readonly=true;
        this.info();
      }else if(this.type=="update"){
        document.title="编辑模板";
        this.readonly=false;
        this.info();
      }
    },
    computed: {
    },
    watch: { 
    },
    methods:{
      info(){
        let vm=this;
        infoTemplate(vm.id).then(response => {
          let res=response.data;
          vm.personLiableID=res.charge_role_id;
          vm.personLiable=res.charge_role_name;
          vm.status=res.status;
          vm.template_name=res.template_name;
          vm.templateType=res.template_type;
          vm.template_note=res.template_note; 
          vm.supervisionData=res.type_st;
          vm.ongetTypeList();
        }).catch(error => {
          vm.$message.error("执行失败！"+error.response.data.error_msg);
        })
      },
      add(){
        let vm=this;
        let params={
          "charge_role_id": Number(vm.personLiableID),
          "charge_role_name": vm.personLiable,
          "status": vm.status,
          "template_name": vm.template_name,
          "template_type": vm.templateType,
          "template_note": vm.template_note,
          "type_st":vm.childData
        } 
        // for(let i=0; i<vm.childData.length; i++){
        //   if(vm.childData[i].st_list.length==0){
        //     vm.$message.info("考核类型至少有一个标准项！");
        //     return;
        //   }
        // }
        addTemplate(params).then(response => {
          vm.$message.success("执行成功！");
          vm.back();
        }).catch(error => {
          vm.$message.error("执行失败！"+error.response.data.error_msg);
        })
      },
      update(){
        let vm=this;
        let params={
          "charge_role_id": Number(vm.personLiableID),
          "charge_role_name": vm.personLiable,
          "status": vm.status,
          "template_name": vm.template_name,
          "template_type": vm.templateType,
          "template_note": vm.template_note,
          "type_st":vm.childData
        }
        updateTemplate(params,vm.id).then(response => {
          vm.$message.success("执行成功！");
          vm.back();
        }).catch(error => {
          vm.$message.error("执行失败！"+error.response.data.error_msg);
        })
      },
      back(){
        this.$router.go(-1);
      },
      onGetRoleList(){//拉取角色职务
        let vm = this;
        vm.roleLoading = true;
        let params = {
          kw: vm.kw_role,
          page_num: 1,
          page_size: 10000
        }; 
        getRoleList(params).then(response => {
          let res = response.data;
          vm.roleDlgList = res.rows;
          vm.roleLoading = false;
        }).catch(error => {
          vm.roleLoading = false;
        })
      },
      onSearchRole(){//职务搜索
        this.onGetRoleList();
      },
      getRole(){//职务框获得焦点时
        let vm=this; 
        vm.onGetRoleList();
        vm.newRoleDialogVisible=true; 
      },
      handleRoleClick(){//职务确认
        let vm=this; 
        let ids="",names="";
        let role = vm.$refs.roleDlg.getCheckedNodes(); 
        if(role.length>0){
          for (var i = 0; i < role.length ; i++) {
            ids+=role[i].role_id+",";
            names+=role[i].role_name+",";
          }
          vm.personLiableID=ids.substring(0,ids.length-1);
          vm.personLiable=names.substring(0,names.length-1);

        }else{
          vm.personLiableID="";
          vm.personLiable="";
        } 
        vm.newRoleDialogVisible=false;
      },
      onChange(a,b,c){
        let vm=this;
        vm.i++;
        if(vm.i%2==0){
          if(b){
            vm.$refs.roleDlg.setCheckedKeys([]);
            vm.$refs.roleDlg.setCheckedKeys([a.role_id]);
          }else{
            vm.$refs.roleDlg.setCheckedKeys([]);
          }
        }
      },
      getChildData(val){
        this.childData=val;
      },
      ongetTypeList(){//类型列表
        let vm=this;
        let params={ 
          page_num:1,
          page_size:10000,
          status:1
        } 
        getTypeList(params).then(response => {
          let res=response.data;
          vm.typeList=res.rows; 
          vm.flag=true;
        }).catch(error => { 
          vm.$message.error("执行失败！"+error.response.data.error_msg);
        })
      },

    }
}
</script>

